var e = new (require("../../../component/calendar-converter/calendar-converter.js").CalendarConverter)(), t = require("../../../component/WxNotificationCenter/WxNotificationCenter.js"), a = require("../../../utils/qiniuUploader"), i = getApp();

Page({
    data: {
        schedule: {},
        isEdit: !1,
        showRoleSelectView: !1,
        images: [],
        token: "",
        scheduleType: 1,
        reservedays: [],
        dateSelector: "",
        timeHadChange: !1
    },
    onLoad: function(e) {
        var t = e.type, a = e.schedule;
        if (a) {
            var i = JSON.parse(a), s = a.length > 0;
            wx.setNavigationBarTitle({
                title: s ? "编辑档期" : "添加档期"
            }), console.log(i), this.setData({
                schedule: i,
                isEdit: s,
                scheduleType: i.type
            });
        } else {
            var n = new Date(), o = n.getFullYear(), c = n.getMonth() + 1, l = n.getDate();
            c < 10 && (c = "0" + c), l < 10 && (l = "0" + l);
            var d = {
                address: "",
                alarms: "[]",
                comment: "",
                company: "",
                contract: "[]",
                images: "",
                plannerName: "",
                plannerPhone: "",
                pricePaid: "",
                priceState: "0",
                priceTotal: "",
                scheduleDate: o + "-" + c + "-" + l,
                time: "2",
                type: t,
                specificTime: "12:00",
                addImages: "",
                reservedTime: 3
            };
            this.setData({
                schedule: d,
                scheduleType: t
            }), this.setLunar(o + "-" + c + "-" + l), this.data.dateSelector = this.selectComponent("#timeSelector"), 
            this.data.dateSelector.show();
        }
        2 == this.data.scheduleType && this.setUpReserveDate(), this.getQiniuToken();
    },
    setUpReserveDate: function() {
        for (var e = [], t = 1; t <= 365; t++) e = e.concat(t + "天");
        console.log(e), this.setData({
            reservedays: e
        });
    },
    clickDate: function(e) {
        this.data.dateSelector = this.selectComponent("#timeSelector"), this.data.dateSelector.show();
    },
    clickRetry: function(e) {
        var t = e.currentTarget.dataset.index, a = this.data.images, i = this.data.images[t];
        this.uploadImageToQiniu(i.path, i.fileName, t), i.state = 0, a[t] = i, this.setData({
            images: a
        });
    },
    changeReserveDay: function(e) {
        console.log(e);
        var t = parseInt(e.detail.value) + 1, a = this.data.schedule;
        a.reservedTime = t, this.setData({
            schedule: a
        });
    },
    bindDateChange: function(e) {
        console.log("picker发送选择改变，携带值为", e.detail);
        var t = this.data.schedule;
        t.scheduleDate = e.detail.date, t.time = e.detail.timepoint, this.setLunar(e.detail.date);
    },
    didSelectDate: function(e) {
        console.log("picker发送选择改变，携带值为", e.detail), this.data.dateSelector.hide();
        var t = this.data.schedule;
        t.scheduleDate = e.detail.date, t.time = e.detail.time, this.setLunar(e.detail.date);
    },
    setLunar: function(t) {
        console.log(t);
        var a, i = new Date(t), s = e.solar2lunar(i), n = s.lunarMonth + "月" + s.lunarDay;
        switch (s.week) {
          case "一":
            a = "1";
            break;

          case "二":
            a = "2";
            break;

          case "三":
            a = "3";
            break;

          case "四":
            a = "4";
            break;

          case "五":
            a = "5";
            break;

          case "六":
            a = "6";
            break;

          case "日":
            a = "7";
        }
        var o = this.data.schedule;
        o.lunar = n, o.week = a, this.setData({
            schedule: o
        });
    },
    bindTimeChange: function(e) {
        console.log("picker发送选择改变，携带值为", e.detail.value);
        var t = this.data.schedule;
        t.specificTime = e.detail.value, this.setData({
            timeHadChange: !0,
            schedule: t
        });
    },
    bindPlannerNameEdit: function(e) {
        var t = this.data.schedule;
        t.plannerName = e.detail.value, this.setData({
            schedule: t
        });
    },
    bindPlannerPhoneEdit: function(e) {
        var t = this.data.schedule;
        t.plannerPhone = e.detail.value, this.setData({
            schedule: t
        });
    },
    bindContactAdd: function(e) {
        this.setData({
            showRoleSelectView: !0
        });
    },
    bindCompanyEdit: function(e) {
        var t = this.data.schedule;
        t.company = e.detail.value, this.setData({
            schedule: t
        });
    },
    bindAddressEdit: function(e) {
        var t = this.data.schedule;
        t.address = e.detail.value, this.setData({
            schedule: t
        });
    },
    bindRoleSelectCancel: function(e) {
        this.setData({
            showRoleSelectView: !1
        });
    },
    commentEditEnd: function(e) {
        var t = this.data.schedule;
        t.comment = e.detail.value, this.setData({
            schedule: t
        });
    },
    bindPaidPriceEdit: function(e) {
        var t = this.data.schedule;
        t.pricePaid = e.detail.value, this.setData({
            schedule: t
        });
    },
    bindTotalPriceEdit: function(e) {
        var t = this.data.schedule;
        t.priceTotal = e.detail.value, this.setData({
            schedule: t
        });
    },
    bindEndPay: function(e) {
        var t = this.data.schedule, a = t.priceState;
        t.priceState = "1" == a ? "0" : "1", this.setData({
            schedule: t
        });
    },
    bindClickRole: function(e) {
        var t = e.currentTarget.dataset.role, a = this.data.schedule, i = a.contract;
        null == i && (i = "[]");
        var s = JSON.parse(i);
        s = s.concat({
            name: "",
            phone: "",
            type: t
        }), i = JSON.stringify(s), a.contract = i, this.setData({
            schedule: a,
            showRoleSelectView: !1
        }), console.log(this.data.schedule);
    },
    bindConfirmContactName: function(e) {
        console.log(e);
        var t = e.currentTarget.dataset.index, a = e.detail.value, i = this.data.schedule, s = JSON.parse(i.contract);
        s[t].name = a, console.log(s);
        var n = JSON.stringify(s);
        i.contract = n, this.setData({
            schedule: i
        }), console.log(this.data.schedule);
    },
    bindConfirmContactPhone: function(e) {
        console.log(e);
        var t = e.currentTarget.dataset.index, a = e.detail.value, i = this.data.schedule, s = JSON.parse(i.contract);
        s[t].phone = a;
        var n = JSON.stringify(s);
        i.contract = n, this.setData({
            schedule: i
        }), console.log(this.data.schedule);
    },
    clickAddImage: function(e) {
        var t = this;
        wx.chooseImage({
            success: function(e) {
                for (var a = e.tempFilePaths, s = t.data.images, n = 0; n < a.length; n++) {
                    var o = i.util.getQiniuFileName(t.data.images.length), c = s.length;
                    t.uploadImageToQiniu(a[n], o, c);
                    var l = {
                        fileName: o,
                        path: a[n],
                        index: c,
                        state: 0
                    };
                    s = s.concat(l);
                }
                t.setData({
                    images: s
                });
            }
        });
    },
    bindImageTap: function(e) {
        for (var t = this.data.schedule.images.split(","), a = 0; a < this.data.images.length; a++) {
            var i = this.data.images[a];
            t = t.concat(i.path);
        }
        wx.previewImage({
            current: e.currentTarget.dataset.url,
            urls: t
        });
    },
    clickDelLocal: function(e) {
        var t = e.currentTarget.dataset.index, a = this.data.images;
        a.splice(t.length, 1), this.setData({
            images: a
        });
    },
    clickDelWeb: function(e) {
        var t = e.currentTarget.dataset.index, a = this.data.schedule.images.split(",");
        a.splice(t, 1);
        var i = this.data.schedule;
        i.images = a.join(","), this.setData({
            schedule: i
        });
    },
    bindSaveClick: function(e) {
        var t = JSON.stringify(this.data.schedule);
        console.log(t), this.saveData();
    },
    saveData: function() {
        var e = this, a = this.data.schedule;
        if (console.log(this.data.images), console.log(this.data.images.length), this.data.images.length > 0) {
            var s = this.getImages();
            if (null == s) return void i.util.showToast("请等图片上传完");
            this.data.isEdit ? a.addImages = s : a.images = s, console.log(s);
        }
        var n = {};
        this.data.isEdit ? (n.scheduleDate = a.scheduleDate, n.company = a.company, n.plannerPhone = a.plannerPhone, 
        n.images = a.images, n.alarms = a.alarms, n.voice = a.voice, n.plannerName = a.plannerName, 
        n.contract = a.contract, n.reservedTime = a.reservedTime, n.kefuAccessToken = "", 
        n.priceState = a.priceState, n.time = a.time, n.specificTime = a.specificTime, n.pricePaid = a.pricePaid, 
        n.priceTotal = a.priceTotal, n.comment = a.comment, n.priceState = a.priceState, 
        n.address = a.address, n.addImages = a.addImages ? a.addImages : "", n.id = this.data.isEdit ? a.scheduleId : "", 
        n.type = this.data.scheduleType) : 0 == this.data.timeHadChange && (a.specificTime = "");
        var o = this.data.isEdit ? i.url.editSchedule : i.url.addSchedule;
        i.network.post({
            url: o,
            params: this.data.isEdit ? n : a,
            before: function() {
                wx.showLoading({
                    title: "正在提交"
                }), wx.showNavigationBarLoading();
            },
            success: function(a) {
                wx.hideLoading(), 1 == a.data.result ? (wx.showToast({
                    title: a.data.message
                }), t.postNotificationName("updateScheduleList"), e.data.isEdit ? setTimeout(function() {
                    wx.navigateBack({
                        delta: 2
                    }), wx.showTabBar({});
                }, 1500) : (setTimeout(function() {
                    wx.switchTab({
                        url: i.globalData.currentTab
                    });
                }, 1200), setTimeout(function() {
                    wx.navigateBack({
                        delta: 2
                    });
                }, 1500))) : i.util.showToast(a.data.message);
            },
            fail: function() {
                wx.hideLoading(), i.util.showToast("处理失败");
            },
            complete: function() {
                wx.hideNavigationBarLoading();
            }
        });
    },
    uploadImageToQiniu: function(e, t, i) {
        var s = this;
        a.upload(e, function(e) {
            console.log("上传res成功", e, t, i);
            var a = s.data.images;
            a[i].state = 1, s.setData({
                images: a
            });
        }, function(e) {
            console.log("error: " + e);
            var t = s.data.images;
            t[i].state = -1, s.setData({
                images: t
            });
        }, {
            region: "ECN",
            uptoken: s.data.token,
            uploadURL: "https://up.qbox.me",
            key: t,
            domain: "i.hunyu.co/"
        }, function(e) {
            console.log("上传res", e), console.log("上传进度", e.progress), console.log("已经上传的数据长度", e.totalBytesSent), 
            console.log("预期需要上传的数据总长度", e.totalBytesExpectedToSend);
        });
    },
    getQiniuToken: function() {
        var e = this;
        i.network.post({
            url: i.url.getQiniuToken,
            params: {},
            before: function() {},
            success: function(t) {
                1 == t.data.result && e.setData({
                    token: t.data.data.uploadToken
                });
            },
            fail: function() {},
            complete: function() {}
        });
    },
    getImages: function() {
        for (var e = "", t = 0; t < this.data.images.length; t++) {
            var a = this.data.images[t];
            if (0 == a.state) return null;
            e = e + a.fileName + ",";
        }
        return e;
    },
    getNewImage: function() {
        for (var e = this.data.schedule.images.split(","), t = [], a = [], i = 0; i < e.length; i++) {
            var s = e[i], n = s.slice(0, 11);
            console.log(n), "http://tmp/" == n ? t = t.concat(s) : a = a.concat(s);
        }
        return console.log("新图", t), console.log("旧图", a), {
            newImages: t,
            oldImages: a
        };
    },
    onReady: function() {},
    onShow: function() {},
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    onShareAppMessage: function() {
        var e = i.util.getShareInfo();
        return {
            title: i.globalData.commonShareTitle,
            path: e[0],
            imageUrl: e[1],
            success: function(e) {},
            fail: function(e) {}
        };
    }
});